package vista;

import controlador.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public class CalculadoraInterfaz extends JFrame{
	/**
	 * 
	 */
	// Variables
	public final int MAX_INPUT_LENGTH = 20;
	public final int INPUT_MODE = 0;
	public final int RESULT_MODE = 1;
	public final int ERROR_MODE = 2;
	private int displayMode;

	private boolean clearOnNextDigit, percent;
	private double lastNumber;
	private String lastOperator;

	private static final long serialVersionUID = 1L;
	
	public JMenu jmenuFile, jmenuHelp;
	public JMenuItem jmenuitemExit, jmenuitemAbout;
	
	public JLabel jlbOutput;
	public JButton jbnButtons[];
	public JPanel jplMaster, jplBackSpace, jplControl;
	
	private CalculadoraControlador control;
		
	private Font f12 = new Font("Arial", 0, 14);
	private Font f121 = new Font("Arial", 1, 14);
	
	public CalculadoraInterfaz(CalculadoraControlador controlador) 
	{
		
		control = controlador;
		
		jmenuFile = new JMenu("Opciones");
		jmenuFile.setFont(f121);
		jmenuFile.setMnemonic(KeyEvent.VK_F);
		
		jmenuitemExit = new JMenuItem("Salir");
		jmenuitemExit.setFont(f12);
		jmenuitemExit.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X, 
													ActionEvent.CTRL_MASK));
		jmenuFile.add(jmenuitemExit);

		jmenuHelp = new JMenu("Ayuda");
		jmenuHelp.setFont(f121);
		jmenuHelp.setMnemonic(KeyEvent.VK_H);

		jmenuitemAbout = new JMenuItem("Acerca de");
		jmenuitemAbout.setFont(f12);
		jmenuHelp.add(jmenuitemAbout);
		
		JMenuBar mb = new JMenuBar();
		mb.add(jmenuFile);
		mb.add(jmenuHelp);
		setJMenuBar(mb);
		
		setBackground(Color.gray);

		jplMaster = new JPanel();

		jlbOutput = new JLabel("0");
		jlbOutput.setHorizontalTextPosition(JLabel.RIGHT);
		jlbOutput.setBackground(Color.WHITE);
		jlbOutput.setOpaque(true);
		
		getContentPane().add(jlbOutput, BorderLayout.NORTH);

		jbnButtons = new JButton[27];

		JPanel jplButtons = new JPanel();

		for (int i=0; i<=9; i++)
		{
			jbnButtons[i] = new JButton(String.valueOf(i));
		}

		jbnButtons[10] = new JButton("+/-");
		jbnButtons[11] = new JButton(".");
		jbnButtons[12] = new JButton("=");
		jbnButtons[13] = new JButton("/");
		jbnButtons[14] = new JButton("*");
		jbnButtons[15] = new JButton("-");
		jbnButtons[16] = new JButton("+");
		jbnButtons[17] = new JButton("sqrt");
		jbnButtons[18] = new JButton("1/x");
		jbnButtons[19] = new JButton("%");
		
		jplBackSpace = new JPanel();
		jplBackSpace.setLayout(new GridLayout(1, 1, 2, 2));

		jbnButtons[20] = new JButton("<");
		jbnButtons[21] = new JButton(" CE ");
		jbnButtons[22] = new JButton("C");

		jplBackSpace.add(jbnButtons[20]);
		jplBackSpace.add(jbnButtons[21]);
		jplBackSpace.add(jbnButtons[22]);

		jplControl = new JPanel();
		jplControl.setLayout(new GridLayout(2, 2, 2 ,2));

		jbnButtons[23] = new JButton("sin x");
		jbnButtons[24] = new JButton("e^x");
		jbnButtons[25] = new JButton("cos x");
		jbnButtons[26] = new JButton("ln x");
		
		jplControl.add(jbnButtons[23]);
		jplControl.add(jbnButtons[24]);
		jplControl.add(jbnButtons[25]);
		jplControl.add(jbnButtons[26]);


		for (int i=0; i<jbnButtons.length; i++)	{
			jbnButtons[i].setFont(f12);

			if (i<10)
				jbnButtons[i].setForeground(Color.darkGray);
			else if (i>=20 && i<=22)
				jbnButtons[i].setForeground(Color.red);
			else if (i>=23 && i<=26)
				jbnButtons[i].setForeground(Color.magenta);
			else
				jbnButtons[i].setForeground(Color.blue);
		}
	
		jplButtons.setLayout(new GridLayout(4, 5, 2, 2));
		
		for(int i=7; i<=9; i++)		{
			jplButtons.add(jbnButtons[i]);
		}
		
		jplButtons.add(jbnButtons[13]);
		jplButtons.add(jbnButtons[17]);
		
		for(int i=4; i<=6; i++)
		{
			jplButtons.add(jbnButtons[i]);
		}
		
		jplButtons.add(jbnButtons[14]);
		jplButtons.add(jbnButtons[18]);

		for( int i=1; i<=3; i++)
		{
			jplButtons.add(jbnButtons[i]);
		}
		
		jplButtons.add(jbnButtons[15]);
		jplButtons.add(jbnButtons[19]);
		
		jplButtons.add(jbnButtons[0]);
		jplButtons.add(jbnButtons[10]);
		jplButtons.add(jbnButtons[11]);
		jplButtons.add(jbnButtons[16]);
		jplButtons.add(jbnButtons[12]);
		
		jplMaster.setLayout(new BorderLayout());
		jplMaster.add(jplBackSpace, BorderLayout.WEST);
		jplMaster.add(jplControl, BorderLayout.EAST);
		jplMaster.add(jplButtons, BorderLayout.SOUTH);

		getContentPane().add(jplMaster, BorderLayout.SOUTH);
		requestFocus();
		
		for (int i=0; i<jbnButtons.length; i++){
			jbnButtons[i].addActionListener(control);
		}
		
		jmenuitemAbout.addActionListener(control);
		jmenuitemExit.addActionListener(control);

		clearAll();

		addWindowListener(new WindowAdapter() {

				public void windowClosed(WindowEvent e)
				{
					System.exit(0);
				}
			}
		);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container contentPane = this.getContentPane();
		this.setTitle("Calculadora");
		this.setSize(241, 217);
		this.pack();
		this.setLocation(400, 250);
		this.setVisible(true);
		this.setResizable(false);
		
	}	

	public void setDisplayString(String s){
		jlbOutput.setText(s);
	}

	public String getDisplayString (){
		return jlbOutput.getText();
	}

	public void clearAll()	{
		setDisplayString("0");
		lastOperator = "0";
		lastNumber = 0;
		displayMode = INPUT_MODE;
		clearOnNextDigit = true;
	}

	public void clearExisting(){
		setDisplayString("0");
		clearOnNextDigit = true;
		displayMode = INPUT_MODE;
	}

	public double getNumberInDisplay()	{
		String input = jlbOutput.getText();
		return Double.parseDouble(input);
	}

	public void displayResult(double result){
		setDisplayString(Double.toString(result));
		lastNumber = result;
		displayMode = RESULT_MODE;
		clearOnNextDigit = true;
	}

	public void displayError(String errorMessage){
		setDisplayString(errorMessage);
		lastNumber = 0;
		displayMode = ERROR_MODE;
		clearOnNextDigit = true;
	}

	public void setClearOnNextDigit(boolean b) {
		clearOnNextDigit = b;
	}

	public int getDisplayMode() {
		return displayMode;
	}

	public boolean getClearOnNextDigit() {
		return clearOnNextDigit;
	}

	public void setDisplayMode(int cod) {
		displayMode = cod;
	}

	
}

